<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/city.json"></script>
<script th:src="@{/js/address.js}"></script>

<head>
    <meta charset="UTF-8">
    <title>后台注册-X-admin2.2</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="stylesheet" th:href="@{/static/login/font.css}">
    <link rel="stylesheet" th:href="@{/static/login/login.css}">
    <link rel="stylesheet" th:href="@{/static/login/xadmin.css}">
    <script type="text/javascript" th:src="@{/static/js/jquery.min.js}"></script>

    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
</head>
<body class="login-bg">
<div class="mask"></div>
<div class="login layui-anim layui-anim-up">
    <div class="message">商家注册</div>
    <div id="darkbannerwrap"></div>
    <form method="post" class="layui-form" id="myform">
        <input type="text" name="shopUser" id="shopUser" placeholder="用户名"><span id="msg"></span><br>
        <input type="password" name="shopPassword" id="shopPassword" placeholder="密码"><span id="msg2"></span><br>
        <input value="注册" style="width:100%" type="submit">
    </form>
    <a th:href="@{/login}" style="float: right">去登录</a>
</div>
</body>
</html>
<script>
    let flag=false;
    $(function () {
        $("#shopPassword").blur(function () {
            if ($("#shopPassword").val()==""){
                $("#msg2").css("color","red")
                $("#msg2").text("密码不能为空!");
            }else {
                $("#msg2").text("");
                flag=true;
            }
        })
        // 处理文本框丢失焦点事件
        $("#shopUser").blur(function() {
            $.ajax({
                type: "POST",
                url: "/selectUser",
                data: {"shopUser": $("#shopUser").val()},
                success: function (result) {
                    //接收后台返回的结果
                    if (result>0) {
                        $("#msg").css("color","red")
                        $("#msg").text("该账号已被注册!");
                        return false
                    }
                    if (result==0) {
                        $("#msg").css("color","green")
                        $("#msg").text("该账号可正常使用!");
                        flag=true;
                    }
                },
                error: function (request) {
                    alert("出错啦");
                    return false;
                }
            })
        });
            $("#myform").submit(function () {
                if (flag){
                    $.ajax({
                        type:"post",
                        url:"/addShop",
                        data:$("#myform").serialize(),
                        success:function(result){
                            if (result>0){
                                alert("注册成功!")
                                window.location.href = "/login"
                            }else {
                                alert("注册失败!")
                                return false;
                            }
                        },
                        error: function (request) {
                            alert("出错啦");
                            return false;
                        }
                    })
                }
                return false
            })

    })
</script>